{-# LANGUAGE OverloadedStrings #-}

-- | The module defining the main Tuura HTML template.
module Pages.MasterPage(masterPage, menu) where

import Text.Blaze.Html5 hiding(footer,menu)
import Prelude hiding(head,div,span)
import qualified Text.Blaze.Html5.Attributes as A
import Session(Session(..))

-- | An HTML menu consisting of to the given list of menu entries
menu :: [(String, String)] -- ^ The list of menu entries, where the first element of a tuple is a href and the second one is a link text.
        -> Html -- ^ The resulting HTML
menu items =
    let menuItem (href,text) = li (a ! A.href (toValue href) $ toHtml text) in
    ul ! A.id "main-menu" $ mapM_ menuItem items

-- | The tuura site main menu
mainMenu :: Html
mainMenu = menu [("/tuura", "Home"),
                 ("/doc", "Docs"),
                 ("/tuura/addProblem", "Add a new problem"),
                 ("/tuura/addSubmission", "Submit solution")]

-- | The tuura site footer
footer :: Html
footer = ""

loginBox :: Maybe Session -> Html
loginBox session = span ! A.class_ "login-info" $ content session
    where
        content (Just (Session userId)) = do
            "You are logged in as: "
            toHtml $ show $ userId
        content Nothing = do
            "You are not logged in. "
            a ! A.href "/tuura/login" $ "Log in"

-- | The main Tuura HTML template
masterPage :: Maybe Session {- ^ The session, needed to display user-specific content -} -> String {- ^ The page title -} -> Html {- ^ The page body -} -> Html {- ^ The whole page -}
masterPage session titleText content = html $ do
    head $ do
        title $ toHtml titleText
        link ! A.href "/theme.css" ! A.rel "stylesheet" ! A.type_ "text/css"
    body $ do
        div ! A.id "top" $ do
            div ! A.id "logo" $ do
                span ! A.class_ "logo-big" $ "Tuura"
                br
                span ! A.class_ "logo-sub" $ "An Algorithm Competition"
            mainMenu
            loginBox session
        content
        footer
